package com.UnionPayCenter.controller;

import com.UnionPayCenter.coupon.dto.CouponChangeValidDTO;
import com.UnionPayCenter.coupon.service.CouponOperatorService;
import com.UnionPayCenter.distribution.dto.DistributionBaseDTO;
import com.UnionPayCenter.distribution.vo.DistributionTemplateVO;
import com.UnionPayCenter.request.PutToUserConditionRequest;
import io.swagger.annotations.ApiParam;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.UnionPayCenter.entity.CouponDeliverRecords;
import com.UnionPayCenter.entity.CouponEntity;
import com.UnionPayCenter.entity.CouponUseRecords;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.entity.UserCouponEntity;
import com.UnionPayCenter.entity.vo.UserCouponVo;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.service.ICouponService;
import com.UnionPayCenter.swagger.ApiJsonObject;
import com.UnionPayCenter.swagger.ApiJsonProperty;
import com.UnionPayCenter.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: UnionPayCenter
 * @description:
 * @author BaoJF
 *
 */
@Slf4j
@Api(value = "coupon", tags = "优惠券")
@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private ICouponService couponService;

    @Autowired
    private CouponOperatorService couponOperatorService;

    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]查询优惠券列表", response = CouponEntity.class)
    public ResponseWrapper findList(
            @ApiJsonObject(name = "couponFindListParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "page", value = "页数", type = "int", required = false),
                    @ApiJsonProperty(key = "pageSize", value = "每页条数", type = "int", required = false),
                    @ApiJsonProperty(key = "name", value = "优惠券名称筛选", required = false),
                    @ApiJsonProperty(key = "validTag", value = "启用状态 1启用 0停用", required = false),
                    @ApiJsonProperty(key = "disType", value = "优惠类型筛选", required = false)
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            PageInfo<CouponEntity> list = couponService.findList(jsonParam);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[后台]查询优惠券列表失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询优惠券列表失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]添加优惠券", response = String.class)
    public ResponseWrapper add(
            @ApiJsonObject(name = "couponAddParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "name", value = "优惠券名称"),
                    @ApiJsonProperty(key = "num", value = "满减金额(单位：分)/折扣数", type = "int"),
                    @ApiJsonProperty(key = "limitTime", value = "是否限时(00限时,01永久有效)"),
                    @ApiJsonProperty(key = "validDays", value = "有效天数", required = false),
                    @ApiJsonProperty(key = "minAmount", value = "起用金额(单位：分)", type = "int", required = false),
                    @ApiJsonProperty(key = "maxAmount", value = "最高优惠金额(单位：分)", type = "int", required = false),
                    @ApiJsonProperty(key = "disType", value = "优惠类型(00满减,01折扣)"),
                    @ApiJsonProperty(key = "limitStock", value = "是否限制库存总量(00限制,01不限)"),
                    @ApiJsonProperty(key = "stock", value = "总库存", type = "int", required = false)
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            String name = jsonParam.getString("name");
            Integer num = jsonParam.getInteger("num");
            Integer validDays = jsonParam.getInteger("validDays");
            Integer minAmount = jsonParam.getInteger("minAmount");
            Integer maxAmount = jsonParam.getInteger("maxAmount");
            String disType = jsonParam.getString("disType");
            String limitStock = jsonParam.getString("limitStock");
            String limitTime = jsonParam.getString("limitTime");
            Integer stock = jsonParam.getInteger("stock");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            if (StringUtils.isNullStr(name)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "name"));
            }
            if (num == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "num"));
            }
            if (!StringUtils.isNumeric(num.toString())) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "num"));
            }
            if (validDays != null && !StringUtils.isNumeric(validDays.toString())) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "validDays"));
            }
            if (StringUtils.isNullStr(disType)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "disType"));
            }
            if (StringUtils.isNullStr(limitStock)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "limitStock"));
            }
            if (StringUtils.isNullStr(limitTime)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "limitTime"));
            }
            if (minAmount != null && !StringUtils.isNumeric(minAmount.toString())) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "minAmount"));
            }
            if (maxAmount != null && !StringUtils.isNumeric(maxAmount.toString())) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "maxAmount"));
            }
            if (stock != null && !StringUtils.isNumeric(stock.toString())) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "stock"));
            }
            couponService.add(jsonParam);
            return ResponseWrapper.succeed("添加成功");
        }catch (ApiException e){
            log.error(String.format("[后台]添加优惠券失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]添加优惠券失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/findCouponInfo", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]查询优惠券详细信息", response = String.class)
    public ResponseWrapper findCouponInfo(
            @ApiJsonObject(name = "findCouponInfoParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "disTypeId", value = "优惠券类型id", type = "int")
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            Integer disTypeId = jsonParam.getInteger("disTypeId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            if (disTypeId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "disTypeId"));
            }
            CouponEntity couponInfo = couponService.findCouponInfo(jsonParam);
            return ResponseWrapper.succeed(couponInfo);
        }catch (ApiException e){
            log.error(String.format("[后台]查询优惠券详细信息失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询优惠券详细信息失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]编辑优惠券", response = String.class)
    public ResponseWrapper update(
            @ApiJsonObject(name = "couponUpdateParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "disTypeId", value = "优惠券类型id", type = "int"),
                    @ApiJsonProperty(key = "limitStock", value = "是否限制库存总量(00限制,01不限)"),
                    @ApiJsonProperty(key = "stock", value = "总库存", type = "int")
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            Integer disTypeId = jsonParam.getInteger("disTypeId");
            String limitStock = jsonParam.getString("limitStock");
            Integer stock = jsonParam.getInteger("stock");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            if (disTypeId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "disTypeId"));
            }
            if (StringUtils.isNullStr(limitStock)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "limitStock"));
            }
            if (stock == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "stock"));
            }
            if (!StringUtils.isNumeric(stock.toString())) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "stock"));
            }
            couponService.update(jsonParam);
            return ResponseWrapper.succeed("编辑成功");
        }catch (ApiException e){
            log.error(String.format("[后台]编辑优惠券失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]编辑优惠券失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }


    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/couponChangeValid", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]优惠券停启信息", response = String.class)
    public ResponseWrapper userCouponList(@ApiParam(value = "优惠券停启信息") @RequestBody CouponChangeValidDTO couponChangeValidDTO){
        if(!("1".equals(couponChangeValidDTO.getValidTag())  || "0".equals(couponChangeValidDTO.getValidTag()))){
            return ResponseWrapper.failed(ErrorCodeEnum.SW100);
        }
        couponOperatorService.changeCouponValidTag(couponChangeValidDTO);
        return ResponseWrapper.succeed();
    }


    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/userCouponList", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]查询用户优惠券列表", response = UserCouponEntity.class)
    public ResponseWrapper userCouponList(
            @ApiJsonObject(name = "userCouponListParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "page", value = "页数", type = "int", required = false),
                    @ApiJsonProperty(key = "pageSize", value = "每页条数", type = "int", required = false),
                    @ApiJsonProperty(key = "mobile", value = "手机号筛选", required = false),
                    @ApiJsonProperty(key = "status", value = "状态筛选", required = false),
                    @ApiJsonProperty(key = "disType", value = "类型筛选(00抵扣券,01折扣券)", required = false),
                    @ApiJsonProperty(key = "deliverTime", value = "投放时间筛选", type = "int", required = false)
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            PageInfo<UserCouponVo> list = couponService.findUserCouponList(jsonParam);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[后台]查询用户优惠券列表失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询用户优惠券列表失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/putToUser", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]投放优惠券", response = String.class)
    public ResponseWrapper putToUser(
            @ApiJsonObject(name = "couponPutToUserParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "mobiles", value = "手机号(若有多个则以逗号拼接)"),
                    @ApiJsonProperty(key = "disTypeId", value = "优惠券类型id", type = "int"),
                    @ApiJsonProperty(key = "count", value = "张数", type = "int"),
                    @ApiJsonProperty(key = "effectiveNextDay", value = "次日生效:1-是，0-否", type = "int")
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            String mobiles = jsonParam.getString("mobiles");
            Integer disTypeId = jsonParam.getInteger("disTypeId");
            Integer count = jsonParam.getInteger("count");
            Integer effectiveNextDay = jsonParam.getInteger("effectiveNextDay");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            if (StringUtils.isNullStr(mobiles)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "mobiles"));
            }
            if (disTypeId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "disTypeId"));
            }
            if (count == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "count"));
            }
            if (!StringUtils.isNumeric(count.toString())) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "count"));
            }
            if (effectiveNextDay == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "effectiveNextDay"));
            }
            String deliverResult = couponService.putToUser(jsonParam);
            return ResponseWrapper.succeed(deliverResult);
        }catch (ApiException e){
            log.error(String.format("[后台]投放优惠券失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]投放优惠券失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/putToConditionUser", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]投放按条件投放优惠券", response = String.class)
    public ResponseWrapper putToResultUser( @ApiParam(value = "分销商户信息") @RequestBody PutToUserConditionRequest putToUserConditionRequest) {
        try {
            Integer managerId = putToUserConditionRequest.getManagerId();
            Integer shopId = putToUserConditionRequest.getShopId();
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            return ResponseWrapper.succeed(couponService.putToConditionUser(putToUserConditionRequest,shopId,managerId));
        }catch (ApiException e){
            log.error(String.format("[后台]投放优惠券失败，参数:%s, 错误信息:%s", JSONObject.toJSONString(putToUserConditionRequest), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]投放优惠券失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }


    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/deliverRecords", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]查询优惠券投放记录", response = UserCouponEntity.class)
    public ResponseWrapper deliverRecords(
            @ApiJsonObject(name = "deliverRecordsParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "page", value = "页数", type = "int", required = false),
                    @ApiJsonProperty(key = "pageSize", value = "每页条数", type = "int", required = false),
                    @ApiJsonProperty(key = "mobile", value = "手机号筛选", required = false),
                    @ApiJsonProperty(key = "disType", value = "优惠券类型筛选(00满减, 01折扣)", required = false),
                    @ApiJsonProperty(key = "startTime", value = "投放开始时间筛选(yyyy-mm-dd)", required = false),
                    @ApiJsonProperty(key = "endTime", value = "投放结束时间筛选(yyyy-mm-dd)", required = false)
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            PageInfo<CouponDeliverRecords> list = couponService.findCouponDeliverRecords(jsonParam);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[后台]查询优惠券投放记录失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询优惠券投放记录失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/exportDelvierRecords", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]优惠券投放记录导出", response = String.class)
    public ResponseWrapper exportDelvierRecords(
            @ApiJsonObject(name = "exportDelvierRecordsParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "mobile", value = "手机号筛选", required = false),
                    @ApiJsonProperty(key = "disType", value = "优惠券类型筛选(00满减, 01折扣)", required = false),
                    @ApiJsonProperty(key = "startTime", value = "投放开始时间筛选(yyyy-mm-dd)", required = false),
                    @ApiJsonProperty(key = "endTime", value = "投放结束时间筛选(yyyy-mm-dd)", required = false)
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            String url = couponService.exportDeliverRecords(jsonParam);
            return ResponseWrapper.succeed(url);
        }catch (ApiException e){
            log.error(String.format("[后台]优惠券投放记录导出失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]优惠券投放记录导出失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/useRecords", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]查询优惠券使用记录", response = UserCouponEntity.class)
    public ResponseWrapper useRecords(
            @ApiJsonObject(name = "useRecordsParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "page", value = "页数", type = "int", required = false),
                    @ApiJsonProperty(key = "pageSize", value = "每页条数", type = "int", required = false),
                    @ApiJsonProperty(key = "mobile", value = "手机号筛选", required = false),
                    @ApiJsonProperty(key = "disType", value = "优惠券类型筛选(00满减, 01折扣)", required = false),
                    @ApiJsonProperty(key = "startTime", value = "使用开始时间筛选(yyyy-mm-dd)", required = false),
                    @ApiJsonProperty(key = "endTime", value = "使用结束时间筛选(yyyy-mm-dd)", required = false)
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            PageInfo<CouponUseRecords> list = couponService.findCouponUseRecords(jsonParam);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[后台]查询优惠券使用记录失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询优惠券使用记录失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/exportUseRecords", method = RequestMethod.POST)
    @ApiOperation(tags = "优惠券", httpMethod = "POST", value = "[后台]优惠券使用记录导出", response = String.class)
    public ResponseWrapper exportUseRecords(
            @ApiJsonObject(name = "exportUseRecordsParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "mobile", value = "手机号筛选", required = false),
                    @ApiJsonProperty(key = "disType", value = "优惠券类型筛选(00满减, 01折扣)", required = false),
                    @ApiJsonProperty(key = "startTime", value = "使用开始时间筛选(yyyy-mm-dd)", required = false),
                    @ApiJsonProperty(key = "endTime", value = "使用结束时间筛选(yyyy-mm-dd)", required = false)
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            String url = couponService.exportUseRecords(jsonParam);
            return ResponseWrapper.succeed(url);
        }catch (ApiException e){
            log.error(String.format("[后台]优惠券使用记录导出失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]优惠券使用记录导出失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
}
